package com.villaday.idf.custom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.model.Sms;
import com.villaday.idf.model.SmsEntity;
import com.villaday.idf.model.V;
import com.villaday.idf.model.vipsGroupsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * 设置分组 对话框
 */
@SuppressLint("ValidFragment")
public class SettingGroupDialog extends BaseDialog implements View.OnClickListener, OkHttpUtil.OnDataListener, TextWatcher {
    TextView tvDiaplayName;
    EditText etGroupName; //设置组名
    TextView tvDisplayRabit;
    EditText etGroupRabit; //设置该组分佣比例
    Context context;

    private vipsGroupsEntity.BodyBean bodyBean;
    private int status;

    private OnSetGroupListener mListener;

    private Gson gson = new GsonBuilder().create();


    public static SettingGroupDialog newInstance(Context context, String decrypt, int status) {
        SettingGroupDialog dialog = new SettingGroupDialog(context);
        Bundle bundle = new Bundle();
        bundle.putString("bean", decrypt);
        bundle.putInt("status", status);
        dialog.setArguments(bundle);
        return dialog;
    }

    public SettingGroupDialog(Context context) {
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_setting_group, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(true);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateTop);
        status = getArguments().getInt("status");
        if (status != 0)
            bodyBean = gson.fromJson(getArguments().getString("bean"), vipsGroupsEntity.BodyBean.class);

        tvDiaplayName = (TextView) view.findViewById(R.id.tv_group_name);
        etGroupName = (EditText) view.findViewById(R.id.et_group_name);

        tvDisplayRabit = (TextView) view.findViewById(R.id.tv_rabit);
        etGroupRabit = (EditText) view.findViewById(R.id.et_rabit);
        TextView btnCancel = (TextView) view.findViewById(R.id.tv_cancel_btn);
        TextView btnSaveGroup = (TextView) view.findViewById(R.id.tv_save_group);

        btnCancel.setOnClickListener(this);
        btnSaveGroup.setOnClickListener(this);

        if (status == 0) { //添加分组
            tvDiaplayName.setVisibility(View.GONE);
            etGroupName.setVisibility(View.VISIBLE);
            tvDisplayRabit.setVisibility(View.GONE);
            etGroupRabit.setVisibility(View.VISIBLE);
        } else {//编辑分组
            if (bodyBean != null) {
                if (bodyBean.isAllowUpdateName()) { //允许修改组名
                    tvDiaplayName.setVisibility(View.GONE);
                    etGroupName.setVisibility(View.VISIBLE);
                    etGroupName.setText(bodyBean.getName());
                } else { //不允许修改组名
                    tvDiaplayName.setVisibility(View.VISIBLE);
                    etGroupName.setVisibility(View.GONE);
                    tvDiaplayName.setText(bodyBean.getName());
                }

                if (bodyBean.isAllowUpdateBrokenPercent()) {//允许修改分佣比例
                    tvDisplayRabit.setVisibility(View.GONE);
                    etGroupRabit.setVisibility(View.VISIBLE);
                    etGroupRabit.setText(bodyBean.getBrokenPercent());
                } else {//不允许修改分佣比例
                    tvDisplayRabit.setVisibility(View.VISIBLE);
                    etGroupRabit.setVisibility(View.GONE);
                    tvDisplayRabit.setText(bodyBean.getBrokenPercent());
                }
            }
        }

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    public void setListener(OnSetGroupListener icListener) {
        this.mListener = icListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_save_group: {  //保存分组

                if (mListener != null) {
                    if (status == 0) { //添加分组
                        if (TextUtils.isEmpty(etGroupName.getText().toString())) {
                            ToastUtil.initToast(context, "请设置组名");
                        } else if (TextUtils.isEmpty(etGroupRabit.getText().toString())) {
                            ToastUtil.initToast(context, "请设置分佣比例");
                        } else {
                            mListener.crateGroup(etGroupName.getText().toString(), etGroupRabit.getText().toString(), 0, null);
                            dismiss();
                        }
                    } else {
                        if (bodyBean.isAllowUpdateName() && TextUtils.isEmpty(etGroupName.getText().toString())) {
                            ToastUtil.initToast(context, "请设置组名");
                        } else if (bodyBean.isAllowUpdateBrokenPercent() && TextUtils.isEmpty(etGroupRabit.getText().toString())) {
                            ToastUtil.initToast(context, "请设置分佣比例");
                        } else {
                            mListener.crateGroup(bodyBean.isAllowUpdateName() ? etGroupName.getText().toString() : tvDiaplayName.getText().toString(),
                                    bodyBean.isAllowUpdateBrokenPercent() ? etGroupRabit.getText().toString() : tvDisplayRabit.getText().toString()
                                    , status, bodyBean.getId());
                            dismiss();
                        }
                    }
                } else {
                    dismiss();
                }
            }
            break;

            case R.id.tv_cancel_btn: {  //取消分组
                dismiss();
            }
            break;
        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.sms.equals(url)) { //刷新Token
                LogeUtil.e("sms", json);
                SmsEntity se = gson.fromJson(json, SmsEntity.class);
                if (Constant.Strings.SUC.equals(se.getCode())) {
                    ToastUtil.initToast(context, se.getMsg());
                } else {
                    ToastUtil.initToast(context, se.getMsg());
                }
            }

        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public interface OnSetGroupListener {
        void crateGroup(String groupName, String groupRabit, int status, String id);
    }


}
